<template>
  <div class="resume_header">
    <span>所有人事</span>
  </div>
  <div class="resume_center">
    
    <el-table :data="filterTableData.slice((page - 1) * limit,page*limit)" style="width: 100%" height="400px" size="large" >
      <el-table-column label="HR姓名" prop="hrName"/>
      <el-table-column label="就职公司" prop="eName"/>
      <el-table-column label="HR级别" prop="hrLevel"/>
      <el-table-column label="HR电话" prop="hrPhone"/>
      <el-table-column label="HR邮箱" prop="hrEmail"/>
      
      <el-table-column align="right">
        <template #header>
          <el-input v-model="search" size="default" placeholder="搜索" />
        </template>
        <template #default="scope">
          <el-button size="default" type="primary" @click="handleInitPwd(scope.$index, scope.row)">初始化密码</el-button>
          <el-button size="default" type="danger" @click="handleDelect(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination">
      <Pagination :page="page" :limit="limit" :handleSizeChange="handleSizeChange" :handleCurrentChange="handleCurrentChange" :filterTableData="filterTableData" :pagesizes="pagesizes" />
    </div>
  </div>

</template>
<script lang='ts' setup>

import { data, elmessage } from "@/hook/hook"
import { ElDrawer, ElMessageBox } from 'element-plus'
import { computed, ref, onMounted, reactive, watch, isProxy, toRef, toRefs, onBeforeMount } from 'vue'
import { useStore } from "vuex"
import {useRouter} from "vue-router"
import {adminGetALLHRs,adminDelectHR,adminInitPwd} from "@/api/adminApi"
import Pagination from "@/components/Pagination.vue"
const store = useStore()
const router=useRouter()


const hrs=store.state.hrState.hrs


// 表格数据定义
interface HR {
  hrId: string
  hrName: string
  hrPhone: string
  hrLevel: string
  hrEmail: string
  eId:string
  eName: string
}
const tableData: HR[] = reactive([])

const search = ref('')
const filterTableData = computed(() =>
  tableData.filter(
    (data) =>
      !search.value ||
      // 通过岗位名称筛选
      data.hrName.toLowerCase().indexOf(search.value.toLowerCase()) !== -1
  )
)

// 初始化和过滤表格内容
function initData() {
  hrs.forEach((value:any,index:any,self:any)=>{
    tableData.push(value)
  })
  
}



// 分页器
const page = ref(1)
const pagesizes=ref([1,2,3,4,10, 20, 30, 40])
const limit = ref(10)

const handleSizeChange = (val: number) => {
  // pushTableData()
  limit.value=val
  page.value=1

}
const handleCurrentChange = (val: number) => {
  page.value=val
  // pushTableData()
}
const handleInitPwd=(index: number, row: HR)=>{
  const newData={
    ...data(),
    initPart:"hr",
    hrId:row.hrId
  }
  adminInitPwd(newData).then((res:any)=>{
    if (res.data["code"] == "200") {
      elmessage(res.data["message"], res.data["type"])
    } else if (res.data["code"] == "300") {
      elmessage(res.data["message"], res.data["type"])
      setTimeout(() => {
        router.replace("/login")
      }, 500);
      localStorage.clear()
    } else {
      console.log(res.data);

    }
  })
}
const handleDelect=(index: number, row: HR)=>{
  const newData={
    ...data(),
    ...row
  }
  adminDelectHR(newData).then((res:any)=>{
    if (res.data["code"] == "200") {
      elmessage(res.data["message"], res.data["type"])
      tableData.splice(index, 1)
    } else if (res.data["code"] == "300") {
      elmessage(res.data["message"], res.data["type"])
      setTimeout(() => {
        router.replace("/login")
      }, 500);
      localStorage.clear()
    } else if(res.data["code"] == "501"){
      elmessage(res.data["message"], res.data["type"])
    }
    else {
      console.log(res.data);

    }
  })
  
  
}

const init=()=>{
  adminGetALLHRs(data()).then((res:any)=>{
    if (res.data["code"] == "200") {
      res.data.datas.forEach((value: any, index: any, self: any) => {
        tableData.push(value)
      })
    } else if (res.data["code"] == "300") {
      elmessage(res.data["message"], res.data["type"])
      setTimeout(() => {
        router.replace("/login")
      }, 500);
      localStorage.clear()
    } else {
      console.log(res.data);

    }
  })
}


onBeforeMount(() => {
  init()

})


</script>
<style scoped>
.pagination{
  float: right;
  margin-right: 50px;
  /* right: 150px; */
}
  
  .addJob_btn{
    margin-right: 100px;
    /* float: right;
    right: 100px; */
   /* margin-left: 700px; */
  }
.resume_center {
  font-family: '微软雅黑';
  height: 800px;
  background-color: #FFF;
}

.resume_header {
  background-color: rgb(215, 215, 215);
  height: 50px;
  display: flex;
  align-items: center;
  padding-left: 20px;
  justify-content: space-between;

}

a {
  text-decoration: none;
  color: #000;
}
</style>